package newcrunch.typography;

/**
 * @author WL
 * 
 * Use to differentiate between different uses of the same
 * identifier / operator
 */
public enum Key 
{
    COLON_LABEL,        //used for switch cases and statement labels
    COLON_OPERATOR,     //used for assertions, ternary operators (boolean ? true : false), and for loops
    DEFAULT_ANNOTATION, //default inside @interface
    DEFAULT_LABEL,      //default: inside switch statements
    SEMICOLON_FOR,      //semi-colon in for loop
    LPAREN_METHOD,      //opening parenthesis after a method name
    RPAREN_CAST,        //closing parenthesis before the expression being casted
    LTYPE,              // <type
    RTYPE,              // type>

    AND_TYPE,           //and used in generics type bounds
    AND,                //binary operator and
    OR,                 //binary operator or
    PIPE,               //used in the new catch clause
    LSHIFT,             // <<
    RSHIFT,             // >>
    URSHIFT,            // >>>    
    LTE,                // <=
    GTE,                // >=    
    LSHIFT_EQ,          // <<=
    RSHIFT_EQ,          // >>=
    URSHIFT_EQ,         // >>>=

    PLUS_PREFIX,        // +a
    SUB_PREFIX,         // -a
    PLUSPLUS_PREFIX,    // ++a
    PLUSPLUS_POSTFIX,   // a++
    SUBSUB_PREFIX,      // --a
    SUBSUB_POSTFIX;      // a--
    
    /**
     * Given the enum key's name, decode to give
     * the original token it is representing.
     * 
     * @param key
     * @return original token, or the same key if
     * it is not found
     */    
    public static String decode(String key)
    {
        try {
            return decode(Key.valueOf(key));
        } catch (IllegalArgumentException e) {
            return key;
        }
    }
    
    /**
     * Given the enum key's name, decode to give
     * the original token it is representing.
     * @param key
     * @return 
     */
    public static String decode(Key key)
    {
        switch (key)
        {
            case COLON_LABEL:
            case COLON_OPERATOR:
                return ":";
                
            case DEFAULT_ANNOTATION:
            case DEFAULT_LABEL:
                return "default";
                
            case SEMICOLON_FOR:
                return ";";
                
            case LPAREN_METHOD:
                return "(";
                
            case RPAREN_CAST:
                return ")";
                
            case LTYPE:
                return "<";
                
            case RTYPE:
                return ">";

            case AND_TYPE:
            case AND:
                return "&";
                
            case OR:
            case PIPE:
                return "|";
                
            case LSHIFT:
                return "<<";
                
            case RSHIFT:
                return ">>";
                
            case URSHIFT:
                return ">>>";
                
            case LTE:
                return "<=";
                
            case GTE:
                return ">=";
                
            case LSHIFT_EQ:
                return "<<=";
                
            case RSHIFT_EQ:
                return ">>=";
                
            case URSHIFT_EQ:
                return ">>>=";

            case PLUS_PREFIX:
                return "+";
                
            case SUB_PREFIX:
                return "-";
                
            case PLUSPLUS_PREFIX:
            case PLUSPLUS_POSTFIX:
                return "++";
                
            case SUBSUB_PREFIX:
            case SUBSUB_POSTFIX:
                return "--";
        }
        
        return "error";
    }
}
